package com.guet.game.airplanes;

import com.guet.game.World;
import com.guet.game.airplanes.dao.FlyingObj;
import com.guet.game.component.BossHpBar;
import com.guet.game.config.GameConfig;
import com.guet.game.utils.Audio;
import com.guet.game.utils.Images;
import com.guet.game.weapon.BossBullet;
import com.guet.game.weapon.Bullet;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

public class BossAirplane extends FlyingObj {

    private int xSpeed;
    private int ySpeed;
    public BossHpBar bsb;
    private int score;

    public BossAirplane() {
        super(112, 128, (World.WIDTH - 112) / 2, -200);
        xSpeed = 2;
        ySpeed = 2;
        currentHp = 100;
        score = 50;
        bsb=new BossHpBar(currentHp);
    }

    @Override
    public BufferedImage getImage() {
        if (state == LIFT) {
            return Images.boss[index++ % 6];
        } else if (state == DEAD) {
            if (boomIndex == Images.booms.length) {
                this.state = REMOVE;
                return null;
            }
            return Images.booms[boomIndex++];
        }
        return null;
    }

    @Override
    public void move() {
        if(y < World.HEIGHT / 6){
            y += ySpeed;
        }else{
            if (x <= 0 || x >= World.WIDTH - this.width) {
                xSpeed = -xSpeed;
            }
            x += xSpeed;
        }
        bsb.gethp(currentHp);
    }

    @Override
    public void paintObj(Graphics g) {
        super.paintObj(g);
        bsb.paintObj(g);
    }

    public void attack(){
        int atk = (int) (Math.random() * 2);

        if(atk == 0){
            shoots();
        }

    }

    int maxBullets = 36;
    double angle = 360 / maxBullets;
    int currentIndex = 0;
    BossBullet[] bullets = new BossBullet[maxBullets];
    boolean shootOver = true;

    public void shoots() {
        index++;
        if(index % 2 == 0 && (bullets[currentIndex] == null || (bullets[currentIndex] != null && bullets[currentIndex].isDead()))){
            bullets[currentIndex] = new BossBullet(this.x + width / 2, this.y + height / 2, currentIndex * angle);
            currentIndex = currentIndex < maxBullets - 1 ? currentIndex + 1 : 0;
        }
    }


    public BossBullet[] getBullets() {
        return bullets;
    }

    public void BulletDead(){
        for (BossBullet eb : bullets) {
            if (eb != null && (eb.getY() > World.HEIGHT || eb.getY() < 0 || eb.getX() < eb.getWidth() || eb.getX() > World.WIDTH)) {
                eb.goDead();
            }
        }
    }

    public boolean ShootOver(){
        return shootOver;
    }

    public void getDamage(int num){
        currentHp-=num;
        if(currentHp < 0){
            goDead();
            World.score += score;
            World.bossComing = false;
            new Audio("boom").Play();
        }
    }

}
